import com.krm.*;

import java.util.List;
import java.util.Scanner;

/**
 *
 * 主入口
 *
 * @author lgw
 *
 * @date 2019-07-22
 */

public class Main {

    public static void main(String[] args) throws  Exception{
        System.out.println("**************************");
        System.out.println("请选择操作");
        System.out.println("1、EXCEL 转 SQL 表结构 ");
        System.out.println("2、EXCEL 转 SQL 插入语句 ");
        System.out.println("3、名字   转 拼音 ");
        System.out.println("**************************");

        int age = myScanner();
        if(age==1||age==2){
            System.out.println("请输入文件路径");
            String  path = myScannerString();
            System.out.println("**************************");
            System.out.println("请选择数据库类型");
            System.out.println("1、Oracle ");
            System.out.println("2、Mysql ");
            System.out.println("**************************");
            int dbType = myScanner();
            if(age==1){
                excelToSql(path.trim(),dbType+"");
            }else {
                excelToSqlInster(path.trim(),dbType+"");
            }
        }
        if(age==3){
            String  emailNet = "";
            System.out.println("生成文件中是否包含Email?  1：是 2：否");
            int flag = myScanner();
            if(flag==1){
                System.out.println("请输入Email公司域（例如：@126.com）");
                emailNet = myScannerString();
            }
            System.out.println("请输入文件路径");
            String  path = myScannerString();

            PinYI.myRun(path.trim(),emailNet.trim());
        }
    }

    public static int myScanner(){
        Scanner sc = new Scanner(System.in);
        int age = sc.nextInt();    //读取整型输入
        if(age!=1&&age!=2&&age!=3){
            System.out.println("输入错误！请重新输入！ ");
            myScanner();
        }
        return age;
    }

    public static String myScannerString(){
        Scanner sc = new Scanner(System.in);
        String age = sc.nextLine();    //读取整型输入
        if("".equals(age)){
            System.out.println("输入错误！请重新输入！ ");
            myScannerString();
        }
        return age;
    }

    public static void excelToSql(String filePath,String dbType) throws  Exception{
        String path_excel = filePath;
//        path_excel = "/test/excelTosql/监管数据集市模型_基线_测试版.xlsx";
//        if(args!=null&&args.length>0){
//            path_excel = args[0];
//        }
        if(path_excel==null||path_excel==""){
            throw new Exception("未得到文件名及路径");
        }

//        System.out.println("------path_excel：  "+path_excel);


        String  systemPathFlag = "\\";
        if(!path_excel.contains(systemPathFlag)){
            systemPathFlag = "/";
//            System.out.println("------/////：  ");
        }
//        System.out.println("------path_excel.lastIndexOf(systemPathFlag)：  "+path_excel.lastIndexOf(systemPathFlag));
        String  path = path_excel.substring(0,path_excel.lastIndexOf(systemPathFlag));
        String fileName = path_excel.substring(path_excel.lastIndexOf(systemPathFlag),path_excel.length());
        fileName = fileName.replace("xlsx","sql");
        fileName = fileName.replace("xls","sql");
        String path_sql = path +fileName;

        ReadExcel re = new ReadExcel();
        List<ExcelBean> list = re.readExcel(path_excel);

        ToSql sql = getToSqlInstall(dbType);
        WriteSqlFile wsf = new WriteSqlFile();
        wsf.WriteSql(path_sql,sql.toSql(list));

        System.out.println("生成SQL文件成功：  "+path_sql);
    }

    public static void  excelToSqlInster(String filePath,String dbType) throws  Exception{
        String path_excel = filePath;
//        path_excel = "/test/excelToSql/QR0002.xlsx";
        if(path_excel==null||path_excel==""){
            throw new Exception("未得到文件名及路径");
        }
        String  systemPathFlag = "\\";
        if(!path_excel.contains(systemPathFlag)){
            systemPathFlag = "/";
        }
        String  path = path_excel.substring(0,path_excel.lastIndexOf(systemPathFlag));
        String fileName = path_excel.substring(path_excel.lastIndexOf(systemPathFlag),path_excel.length());
        fileName = fileName.replace("xlsx","sql");
        fileName = fileName.replace("xls","sql");
        String path_sql = path +fileName;

        ReadExcel re = new ReadExcel();
        List<SQLInsterBean> list = re.readExcelForSqlInster(path_excel);
        ToSql sql = getToSqlInstall(dbType);
        WriteSqlFile wsf = new WriteSqlFile();
        wsf.WriteSql(path_sql,sql.toSqlInster(list));

        System.out.println("生成SQL文件成功：  "+path_sql);
    }

    public static ToSql getToSqlInstall(String dbType){
        switch (dbType){
            case "1":
                return  new ToOracleSql();
            case "2":
                return  new ToMySql();
            default:
                return  new ToOracleSql();
        }
    }

}
